<template>
  <div v-loading="loading" class="list-table">
    <h2 style="text-align: center">{{ $t('基金清算') }}</h2>
    <el-form ref="form" :model="liquidation" :rules="rules" label-width="120px" class="dataForm">
      <el-table :data="liquidation.fundLiquidations" border class="nowrapTable formTable tree_el_Table">
        <el-table-column :label="$t('事项名称')" align="center" prop="itemName">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('事项名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'fundLiquidations.' + scope.$index + '.itemName'" :rules="[{ required: true, message: $t('必填信息'), trigger: ['blur', 'change'] }]">
              <el-input v-model="scope.row.itemName" :disabled="disabledBtn(scope.row.templateId)" show-word-limit maxlength="100" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('开始时间')" align="center" prop="startDate">
          <template slot-scope="scope">
            <DatePicker
              v-model="scope.row.startDate"
              :placeholder="$t('开始时间')"
              @change="dateChange($event, scope.$index)"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('预计完成时间')" align="center" prop="endDate">
          <template slot-scope="scope">
            <DatePicker
              v-model="scope.row.endDate"
              :placeholder="$t('预计完成时间')"
              @change="dateChange($event, scope.$index)"
            />
          </template>
        </el-table-column>
        <el-table-column :label="$t('负责人')" align="center" prop="personRange">
          <template slot-scope="scope">
            <user-select v-model="scope.row.personRange" :placeholder="$t('负责人')" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" prop="remark">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" type="textarea" :autosize="{ minRows: 2 }" show-word-limit maxlength="2000" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('是否完成')" align="center" prop="complete" width="110">
          <template slot-scope="scope">
            <el-radio-group v-model="scope.row.complete" size="mini">
              <el-radio-button
                v-for="(dict, index) in yesOrNoOptions"
                :key="index"
                :label="dict.dictValue"
              >{{ $t(dict.dictLabel) }}</el-radio-button>
            </el-radio-group>
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" width="80px" align="center">
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="deleteHandle(scope.$index)"
            >{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addLiquidation">{{ $t("添加") }}</el-button>
      </div>
      <FileTable :list.sync="liquidation.fileList" data-type="fund_liquidation" :business-id="autoBusinessId($route.query.id, form)" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;margin-top: 20px">
      <el-button v-hasPermi="['fund:liquidation:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>
  import { getDicts } from '@/api/system/dict/data'
  import { saveLiquidation, listFundLiquidation } from '@/api/fund/liquidationtemplate/fundLiquidation'
  import { initialization } from '@/api/fund/liquidationtemplate/fundLiquidationTemplate'
  export default {
    data() {
      return {
        otherBtnHide: false,
        // 蒙层
        loading: false,
        // 基金清算数据
        liquidation: {
          fundLiquidations: []
        },
        // 是否字典
        yesOrNoOptions: [],
        rules: {
          itemName: [
            { required: true, message: this.$t('必填信息'), trigger: ['blur', 'change'] }
          ]
        }
      }
    },
    created() {
      this.init()
      getDicts('sys_yes_no').then(response => {
        this.yesOrNoOptions = response.data
      })
    },
    methods: {
      disabledBtn(val) {
        return !!val
      },
      init() {
        this.loading = true
        listFundLiquidation(this.$route.query.id).then(response => {
          if (response.data.fundId !== null) {
            this.liquidation = response.data
            this.loading = false
          } else {
            initialization().then(response => {
              this.liquidation = response.data
              this.liquidation.fileList = []
              this.liquidation.fundId = this.$route.query.id
              this.loading = false
            })
          }
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.loading = true
            saveLiquidation(this.liquidation).then(response => {
              if (response.code === 200) {
                this.msgSuccess(this.$t('保存成功'))
              } else {
                this.msgError(response.msg)
              }
              this.loading = false
            })
          } else {
            this.locationError(document)
          }
        })
      },
      /** 删除按钮操作 */
      deleteHandle(index) {
        this.liquidation.fundLiquidations.splice(index, 1)
      },
    // 添加其它资金table行
    addLiquidation() {
      const row = {
        id: undefined,
        templateId: undefined,
        itemName: undefined,
        startDate: undefined,
        endDate: undefined,
        amount: undefined,
        personRange: undefined,
        remark: undefined,
        complete: undefined
      }
      if (!(this.liquidation.fundLiquidations instanceof Array)) {
        this.$set(this.liquidation, 'fundLiquidations', [])
      }
      this.$nextTick(() => {
        this.liquidation.fundLiquidations.push(row)
      })
    },
    dateChange(val, index) {
      if (this.liquidation.fundLiquidations[index].startDate && this.liquidation.fundLiquidations[index].endDate) {
        const startDate = new Date(this.liquidation.fundLiquidations[index].startDate)
        const endDate = new Date(this.liquidation.fundLiquidations[index].endDate)
        if (startDate.getTime() > endDate.getTime()) {
          this.$message({
            type: 'warning',
            message: this.$t('开始时间不能大于预计完成时间')
          })
          this.$set(this.liquidation.fundLiquidations[index], 'startDate', undefined)
          this.$set(this.liquidation.fundLiquidations[index], 'endDate', undefined)
        }
      }
    }
    }
  }
</script>

<style scoped>

</style>
